package algorithms.leecode;

/**
 * 不会，好tm难
 */
public class Au87 {
    public boolean isScramble(String s1, String s2) {
        int len = s1.length();
        if (len == 1) {
            return s1.charAt(0) == s2.charAt(0);
        }
        boolean[][][] dp = new boolean[len][len][len + 1];
        //初始化
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                dp[i][j][1] = s1.charAt(i) == s2.charAt(j);
            }
        }
        //dp迭代
        for (int i = len - 1; i >= 0; i--) {
            for (int j = len - 1; j >= 0; j--) {
                int maxLen = Math.min(len - i, len - j);
                for (int l = 2; l <= maxLen; l++) {
                    dp[i][j][l] = false;
                    for (int k = 1; k < l; k++) {
                        if (dp[i][j][k] && dp[i + k][j + k][l - k] || dp[i][j + l - k][k] && dp[i + k][j][l - k]) {
                            dp[i][j][l] = true;
                            break;
                        }
                    }
                }
            }
        }
        return dp[0][0][len];
    }
}
